package com.dgut.legalservice.controller;

import com.dgut.legalservice.domain.*;
import com.dgut.legalservice.service.AreaService;
import com.dgut.legalservice.service.CompanyService;
import com.dgut.legalservice.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private AreaService areaService;
    /**
     * 下订单
     * @param order
     * @param productId
     * @return
     */
    @RequestMapping("/order/{productId}")
    public ModelAndView addOrder(Order order,@PathVariable String productId,HttpSession session){
        User user =(User) session.getAttribute("user");
        int userId = user.getId();
        order.setUserId(userId);//设置用户id
        Double uuid=((Math.random()*9+1)*100000);
        String s = Double.toString(uuid);
        order.setOrderNum(s);
        order.setOrderName("公司核名");
        order.setServiceType("公司服务");
//        order.setSendOrderStatus("");
        order.setStatus("平台方未审核");//订单审核状态
        order.setAcceptStatus("未受理");//是否受理
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setSendOrderStatus("未发布订单");//订单未发布
        order.setCreateBy(1);
        order.setUpdateBy(1);
        order.setFlowStatus("核名");
        order.setPayNum("242343242");
        order.setPayStatus("已支付");
        order.setNextStatus("公司设立");
        ModelAndView mv = new ModelAndView();
        List<Integer> list = orderService.addOrder(order, productId);
//        System.out.println("公司的主键id"+list.get(1));
        mv.addObject("id",list.get(1));
        System.out.println(Integer.parseInt(productId));
        if ("5".equals(productId)) {
            mv.setViewName("/Front-Admin/companyServiceFront/purchaseSuccess.html");
        }else if ("2".equals(productId)){
            mv.setViewName("/Front-Admin/companyServiceFront/purchaseSuccess1.html");
        }else if ("3".equals(productId)){
            mv.setViewName("/Front-Admin/companyServiceFront/purchaseSuccess2.html");
        }else {
            mv.setViewName("/Front-Admin/companyServiceFront/purchaseSuccess3.html");
        }
//        mv.setViewName("/Front-Admin/companyServiceFront/purchaseSuccess.html");
        return mv;
    }

    @RequestMapping("/help")
    public ModelAndView help(String name,String id){
        ModelAndView mv = new ModelAndView();
        if ("check".equals(name)){
            System.out.println(id);
            mv.addObject("id",id);
            mv.setViewName("/Front-Admin/companyServiceFront/checkName.html");
        }else if ("regist".equals(name)){
            System.out.println(id);
            mv.addObject("id",id);
            mv.setViewName("/Front-Admin/companyServiceFront/registerCompany.html");
        } else if ("seal".equals(name)) {
            System.out.println(id);
            mv.addObject("id",id);
            mv.setViewName("/Front-Admin/companyServiceFront/sealmake.html");
        }else {
            System.out.println(id);
            mv.addObject("id",id);
            mv.setViewName("/Front-Admin/companyServiceFront/openBankAccount.html");
        }

        return mv;
    }
    /**
     * 查询用户是否一键购买了订单
     */
    @ResponseBody
    @RequestMapping("/if")
    public String findAll(OrderVo orderVo, HttpSession session){

        User user = (User)session.getAttribute("user");

        int userId = user.getId();
        orderVo.setUserId(userId);
        orderVo.setProductName("一键购买");
        List<Order> orders = orderService.selectOrders(orderVo);

        for (int i = 0; i < orders.size(); i++) {
            if (orders.get(i).getProducts().size()!=0){
                return "一键购买";
            }
            else{
                continue;
            }
        }

        orderVo.setProductName("公司核名");
        List<Order> orders1 = orderService.selectOrders(orderVo);
        for (int i = 0; i < orders1.size(); i++) {
            if (orders1.get(i).getProducts().size()!=0){
                return "公司核名";
            }
            else{
                continue;
            }
        }

        orderVo.setProductName("公司新设");
        List<Order> orders2 = orderService.selectOrders(orderVo);
        for (int i = 0; i < orders2.size(); i++) {
            if (orders2.get(i).getProducts().size()!=0){
                return "公司新设";
            }
            else{
                continue;
            }
        }

        orderVo.setProductName("印章刻制");
        List<Order> orders3 = orderService.selectOrders(orderVo);
        for (int i = 0; i < orders3.size(); i++) {
            if (orders3.get(i).getProducts().size()!=0){
                return "印章刻制";
            }
            else{
                continue;
            }
        }

        orderVo.setProductName("银行开户");
        List<Order> orders4 = orderService.selectOrders(orderVo);
        for (int i = 0; i < orders4.size(); i++) {
            if (orders4.get(i).getProducts().size()!=0){
                return "银行开户";
            }
            else{
                continue;
            }
        }
        return "没有购买";
    }

    /**
     * 添加或更新公司核名
     * @param id

     * @return
     */
    @RequestMapping(value = "/name/{id}")
    public ModelAndView addComponyName(AreaVo areaVo, @PathVariable String id) {
        System.out.println(areaVo);
        System.out.println(id);
        ModelAndView mv = new ModelAndView();
        int i = companyService.addComponyName(areaVo.getCompany(),id);

        mv.setViewName("redirect:/user/header.html");

        return mv;
    }

    /**
     * 添加或更新公司设立
     * @param company
     * @param id
     * @return
     */
    @RequestMapping("/creat/{id}")
    public ModelAndView addCompanyCreat(Company company , @PathVariable String id){
        System.out.println(company);
        ModelAndView mv = new ModelAndView();
        int i = companyService.addComponyCreat(company, id);

        mv.setViewName("redirect:/user/header.html");
        return mv;
    }

    /**
     * 添加或更新公司印章
     * @param company
     * @param id
     * @return
     */
    @RequestMapping("/seal/{id}")
    public ModelAndView addCompanySeal(Company company,@PathVariable String id){
        ModelAndView mv = new ModelAndView();
        int i = companyService.addComponySeal(company,id);

        mv.setViewName("redirect:/user/header.html");
        return mv;
    }

    /**
     * 添加或更新银行开户
     * @param company
     * @param id
     * @return
     */
    @RequestMapping("/bank/{id}")
    public ModelAndView addCompanyBank(Company company,@PathVariable String id){
        ModelAndView mv = new ModelAndView();
        int i = companyService.addComponyBank(company, id);

        mv.setViewName("redirect:/user/header.html");

        return mv;
    }

}
